package com.cskaoyan.controller.wxlogin;

import com.cskaoyan.bean.bo.RegCaptchaBO;
import com.cskaoyan.bean.bo.RegisterBO;
import com.cskaoyan.bean.bo.ResetBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.LoginUserData;
import com.cskaoyan.service.WXAuthService;
import com.cskaoyan.utils.RedisUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("wx/auth")
public class WXAuthController {


    @Autowired
    WXAuthService wxAuthService;
    @Autowired
    private ServletContext servletContext;
    @Autowired
    RedisUtils redisUtils;


    @PostMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody @Validated RegCaptchaBO regCaptchaBO) {
        // String mobile = (String) map.get("mobile");
        String mobile = regCaptchaBO.getMobile();
        // 生成一个6位验证码
        String captcha = "";
        for (int i = 0; i < 6; i++) {
            captcha += new Random().nextInt(10);
        }
        // 把验证码放到session里
//        servletContext.setAttribute(mobile, captcha);
        redisUtils.set(mobile, captcha, 30L);

        return BaseRespVo.ok("验证码一分钟内有效");
    }

    @PostMapping("register")
    public BaseRespVo register(@Validated @RequestBody RegisterBO registerBO) {

        // 验证验证码
//        String captcha = (String) servletContext.getAttribute(registerBO.getMobile());
        String captcha = (String) redisUtils.get(registerBO.getMobile());
        if (captcha == null) {
            return BaseRespVo.invalidParameter("验证码已失效");
        } else {
            if (!registerBO.getCode().equals(captcha)) {
                return BaseRespVo.invalidParameter("验证码错误");
            }
        }

        LoginUserData loginUserData = null;

        loginUserData = wxAuthService.register(registerBO);


        return BaseRespVo.ok(loginUserData);
    }

    @PostMapping("reset")
    public BaseRespVo reset(@RequestBody ResetBO resetBO) {
        // 验证验证码
        String captcha = (String) servletContext.getAttribute(resetBO.getMobile());
        if (captcha == null) {
            return BaseRespVo.invalidParameter("验证码已失效");
        } else {
            if (!resetBO.getCode().equals(captcha)) {
                return BaseRespVo.invalidParameter("验证码错误");
            }
        }
        wxAuthService.reset(resetBO);
        return BaseRespVo.ok();
    }


    @PostMapping("login")
    public BaseRespVo login(@RequestBody Map map) {

        String username = (String) map.get("username");
        String password = (String) map.get("password");

        LoginUserData loginUserData = null;
        loginUserData = wxAuthService.login(username, password);


        return BaseRespVo.ok(loginUserData);

    }

    @PostMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return BaseRespVo.ok();
    }
}
